package com.aiju.common.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.aiju.bean.Good;

import java.util.ArrayList;
import java.util.List;

public class GoodService {
    private DBOpenHelper openHelper;

    public GoodService(Context context) {
        openHelper = new DBOpenHelper(context);
    }

    /**
     * 根据二维码查询数据
     *
     * @param code
     * @return
     */
    public List<Good> getCodeGood(String code) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from good where code=?", new String[]{code});
        List<Good> goods = new ArrayList<>();
        while (cursor.moveToNext()) {
            Good good = new Good();
            good.setId(cursor.getString(cursor.getColumnIndex("goodid")));
            good.setName(cursor.getString(cursor.getColumnIndex("name")));
            good.setCode(cursor.getString(cursor.getColumnIndex("code")));
            good.setPrice(cursor.getString(cursor.getColumnIndex("price")));
            good.setUnit(cursor.getString(cursor.getColumnIndex("unit")));
            good.setImageUrl(cursor.getString(cursor.getColumnIndex("imageurl")));
            good.setSelectSum(cursor.getInt(cursor.getColumnIndex("selectsum")));
            good.setGoodsTypeId(cursor.getString(cursor.getColumnIndex("goodstypeid")));
            goods.add(good);
        }
        cursor.close();
        db.close();

        return goods;
    }

    public List<Good> getLikeGood(String like) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from good where code like ? or name like ?", new String[]{"%" + like + "%", "%" + like + "%"});
        List<Good> goods = new ArrayList<>();
        while (cursor.moveToNext()) {
            Good good = new Good();
            good.setId(cursor.getString(cursor.getColumnIndex("goodid")));
            good.setName(cursor.getString(cursor.getColumnIndex("name")));
            good.setCode(cursor.getString(cursor.getColumnIndex("code")));
            good.setPrice(cursor.getString(cursor.getColumnIndex("price")));
            good.setUnit(cursor.getString(cursor.getColumnIndex("unit")));
            good.setImageUrl(cursor.getString(cursor.getColumnIndex("imageurl")));
            good.setSelectSum(cursor.getInt(cursor.getColumnIndex("selectsum")));
            good.setGoodsTypeId(cursor.getString(cursor.getColumnIndex("goodstypeid")));
            goods.add(good);
        }
        cursor.close();
        db.close();
        return goods;
    }

    /**
     * 保存商品集合
     *
     * @param goods
     */
    public void saveGood(List<Good> goods) {
        for (Good good : goods) {
            if (isHasData(good)) {
                updateGood(good);
            } else {
                saveGood(good);
            }
        }
    }

    /**
     * 保存商品集合
     *
     * @param good
     */
    public void saveGood(Good good) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.beginTransaction();
        try {
            db.execSQL("insert into good(goodid, name, price, unit, code, imageurl, selectsum, goodstypeid) VALUES(?,?,?,?,?,?,?,?)",
                    new Object[]{good.getId(), good.getName(), good.getPrice(), good.getUnit(), good.getCode(), good.getImageUrl(),
                            good.getSelectSum(), good.getGoodsTypeId()});
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        db.close();
    }

    /**
     * 更新商品
     *
     * @param good
     */
    public void updateGood(Good good) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.beginTransaction();
        try {
            db.execSQL("update good set name=?, price=?, unit=?, code=?, imageurl=?, selectsum=?, goodstypeid=? where goodid=?",
                    new Object[]{good.getName(), good.getPrice(), good.getUnit(), good.getCode(), good.getImageUrl(), good.getSelectSum(), good.getGoodsTypeId(), good.getId()});
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
        db.close();
    }

    /**
     * 删除商品
     *
     * @param good
     */
    public void deleteGood(Good good) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        db.execSQL("delete from good where goodid=?", new Object[]{good.getId()});
        db.close();
    }

    /**
     * 是否有记录
     *
     * @param good
     */
    public boolean isHasData(Good good) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select name from good where goodid=?", new String[]{good.getId()});
        if (cursor == null) {
            return false;
        }
        if (cursor.getCount() == 0) {
            return false;
        } else {
            return true;
        }
    }

    public Good getGood(String goodid) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from good where goodid=?", new String[]{goodid});
        Good good = new Good();
        while (cursor.moveToNext()) {
            good.setId(cursor.getString(cursor.getColumnIndex("goodid")));
            good.setName(cursor.getString(cursor.getColumnIndex("name")));
            good.setCode(cursor.getString(cursor.getColumnIndex("code")));
            good.setPrice(cursor.getString(cursor.getColumnIndex("price")));
            good.setUnit(cursor.getString(cursor.getColumnIndex("unit")));
            good.setImageUrl(cursor.getString(cursor.getColumnIndex("imageurl")));
            good.setSelectSum(cursor.getInt(cursor.getColumnIndex("selectsum")));
            good.setGoodsTypeId(cursor.getString(cursor.getColumnIndex("goodstypeid")));
        }
        cursor.close();
        db.close();
        return good;
    }

    public void deleteAll() {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        db.execSQL("delete from good");
    }
}
